<?php
/**
 * 后台帐号表
 *
 * @author     mole<mole1230@gmail.com>
 * @version    $Id: Admin.php 86 2011-01-13 09:23:11Z mole1230 $
 *
 * The followings are the available columns in table 'designer.admin':
 * @property integer $id
 * @property integer $admin_role_id
 * @property string $username
 * @property string $password
 * @property string $realname
 * @property string $create_time
 * @property string $update_time
 */
class Admin extends CActiveRecord
{	
	const SUPER_ADMIN_ID = 1;
	
	public $repassword;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Admin the static model class
	 */
	public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{admin}}';
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, realname', 'required', 'on' => 'insert, update'),
			array('password, repassword', 'required', 'on' => 'password'),
			array('admin_role_id', 'numerical', 'integerOnly' => true), 
			array('username, realname', 'length', 'max' => 25), 
			array('password', 'length', 'max' => 32), 
			array('password', 'compare', 'compareAttribute' => 'repassword', 'on' => 'password'),
			array('username', 'application.extensions.jformvalidate.ECustomJsValidator',
				'on' => 'insert',
				'rules' => array('remote' => '/admin/unique'),
				'messages' => array('{attribute} already registred')
			),
		);
	}
	
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'role' => array(self::BELONGS_TO, 'AdminRole', 'admin_role_id')
		);
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID', 
			'admin_role_id' => '角色', 
			'username' => '用户名', 
			'password' => '密码', 
			'repassword' => '确认密码',
			'realname' => '真实姓名', 
			'create_time' => 'Create Time', 
			'update_time' => 'Update Time'
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * 
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($rank, $limit = 20)
	{
		$criteria = new CDbCriteria();
		$criteria->compare('admin_role_id', " < {$rank}");
		$criteria->order = 'id DESC';
		
		return new CActiveDataProvider(get_class($this), 
			array(
				'criteria' => $criteria,
				'pagination' => array(
					'pageSize' => $limit
				)
			));
	}
	
	public function validatePassword($password)
	{
		return $this->hashPassword($password) === $this->password;
	}
	
	public function hashPassword($password)
	{
		return md5($password);
	}
	
	protected function beforeSave()
	{
		if (parent::beforeSave()) {
			if ($this->isNewRecord) {
				$this->create_time = TIMEDATE;
				$this->update_time = TIMEDATE;
				$this->password = $this->hashPassword(TIMESTAMP);
			} else {
				$this->update_time = TIMEDATE;
				if ($this->getScenario() == 'password') {
					$this->password = $this->hashPassword($this->password);
				}
			}
			
			return true;
		}
		
		return true;
	}
}
